package com.zhan.ielts.web.controller.speakingmodify;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.utils.BeanUtils;
import com.zhan.frame.core.utils.DateUtils;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.Result;

import com.zhan.ielts.domain.comment.Comment;
import com.zhan.ielts.domain.query.speakingmodify.SpeakingModifyQuestionQuery;
import com.zhan.ielts.domain.speakingmodify.SpeakingModifyAudio;
import com.zhan.ielts.domain.speakingmodify.SpeakingModifyQuestion;
import com.zhan.ielts.domain.vo.speakingmodify.SpeakingModifyQuestionVo;
import com.zhan.ielts.service.comment.ICommentService;
import com.zhan.ielts.service.speakingmodify.ISpeakingModifyAudioService;
import com.zhan.ielts.service.speakingmodify.ISpeakingModifyQuestionService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mapping.model.IllegalMappingException;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * Created by tlfu on 2017/1/11.
 */
@Controller
@RequestMapping("/speakingmodify/question")
public class SpeakingModifyQuestionController extends BaseControllerImpl<SpeakingModifyQuestion, SpeakingModifyQuestion> {
    @Autowired
    private ISpeakingModifyQuestionService speakingModifyQuestion;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private ISpeakingModifyAudioService speakingModifyAudioService;

    @Override
    protected BaseService<SpeakingModifyQuestion> getBaseService() {
        return speakingModifyQuestion;
    }

    @RequestMapping(value = "/selectPageList", method = RequestMethod.GET)
    public ModelAndView selectList(SpeakingModifyQuestionQuery query, @PageableDefault Pageable pageable) {
        Page<SpeakingModifyQuestion> page = speakingModifyQuestion.selectPageList(query, pageable);
        ModelAndView mav = new ModelAndView("/speakingmodify/question/listQuestion", "page", page);
        mav.addObject("query", query);
        return mav;
    }

    @Override
    @RequestMapping(value = "/addView", method = RequestMethod.GET)
    public ModelAndView addView() {
        return new ModelAndView("/speakingmodify/question/editQuestion");
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public ModelAndView editView(@PathVariable String id) {
        SpeakingModifyQuestion result = speakingModifyQuestion.queryById(id);
        Map map = BeanUtils.toMap(result);
        return new ModelAndView("/speakingmodify/question/editQuestion", map);
    }

    @ResponseBody
    @RequestMapping(value = "/addEntity", method = RequestMethod.POST)
    public Result addEntity(SpeakingModifyQuestion question) {
        try {
            if (StringUtils.isEmpty(question.getContent())) {
                throw new IllegalMappingException("question is null!!");
            }
            if (question.getPublishTime() == null) {
                throw new IllegalMappingException("publishTime is null!!");
            }
            question.setCreateTime(new Date());
            question.setUpdateTime(question.getCreateTime());
            if (StringUtils.isEmpty(question.getId())) {
                SpeakingModifyQuestion query = new SpeakingModifyQuestion();
                query.setPublishTime(question.getPublishTime());
                Long count = speakingModifyQuestion.queryCount(query);
                if (count != null && count > 0) {
                    return new Result(Result.Status.ERROR, DateUtils.dateToString(query.getPublishTime(), DateUtils.SHORTFOMAT_MINUS)+"已经存在记录");
                }
                speakingModifyQuestion.insert(question);
            } else {
                speakingModifyQuestion.updateById(question);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.Status.ERROR, e.getMessage());
        }
        return new Result(Result.Status.OK, "success");
    }

    @Override
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Result deleteList(String[] ids) {
        if (ArrayUtils.isEmpty(ids)) {
            return new Result(Result.Status.ERROR, "没有传入要删除的ID号数组！");
        }
        try {
            for (String id : ids) {
                deleteOne(id);
            }
        } catch (Exception e) {
            return new Result(Result.Status.ERROR, "批量删除失败！");
        }
        return new Result(Result.Status.OK, ids.length);
    }

    @Override
    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Result deleteOne(@PathVariable("id") String id) {
        if (org.apache.commons.lang3.StringUtils.isBlank(id)) {
            return new Result(Result.Status.ERROR, "没有传入要删除的ID号！");
        }
        SpeakingModifyAudio query = new SpeakingModifyAudio();
        query.setQuestionId(Integer.parseInt(id));
        List<SpeakingModifyAudio> audios = speakingModifyAudioService.queryList(query);
        if (!CollectionUtils.isEmpty(audios)) {
            for (SpeakingModifyAudio audio : audios) {
                Comment comment = new Comment();
                comment.setResourceId(audio.getId());
                comment.setCommentType(1);
                comment.setCommentModule(1);
                List<Comment> comments = commentService.queryList(comment);
                if (!StringUtils.isEmpty(comments)) {
                    for (Comment c : comments) {
                        commentService.deleteById(c.getId());
                    }
                }
                speakingModifyAudioService.deleteById(audio.getId());
            }
        }
        int count = getBaseService().deleteById(id);
        if (count == 0)
            return new Result(Result.Status.ERROR, "要删除的记录不存在！");
        return new Result(Result.Status.OK, count);
    }

}
